<?php

class CF_Util {


// THis function wil parce fields for CSV output.
// from http://www.wlscripting.com/tutorial/37
// Modified to use strstr instead of iregi, and added codes 10, 13 and 12 instead of newline
static function csvEscapeText($comments) {
  $comments = str_replace('"', '""', $comments); // First off escape all " and make them ""
 // if(eregi(",", $comments) or eregi("\n", $comments) or eregi("\r", $comments)) { // Check if I have any commas or new lines
 if(strstr($comments, ",") or strstr($comments, 10)or strstr($comments, 13)or strstr($comments, 12)){
    return '"'.$comments.'"'; // If I have new lines or commas escape them
  } else {
    return $comments; // If no new lines or commas just return the value
  }
}
	/**
	 * GetPersistent will return a value for a variable name (varname)
     * The value will either come from  one of the server arrays (REQUEST, etc)
     * If a variable is not set in the server array, it will be the last value used, stored in the SESSION.
     * When values are recieved, they will be stored in the session.
     * By defalut, the $_REQUEST server array is used.  This can be set by the type {get, post, req}
     * @param string $varname The name of the variable as stored in the server arrays and SESSION
     * @param string $type The server arry to use {'get', 'post', 'req'} defaults to request
     * @param string $unsetvar The value for which the variable is to be unset on if it is the server array value.  defulats to '' (empty string)
	 */
static function GetPersistent($varname,$type = 'req', $unsetval = ''){
if($type == 'get'){
	if(isset($_GET[$varname])){
			if($_GET[$varname] == $unsetvar){
				unset($_SESSION[$varname]);
				return NULL;
			} else{ // end got unsetvar
				$_SESSION[$varname] = $_GET[$varname];
				return $_GET[$varname];
			}
		} else { // end GET is set
			if(isset($_SESSION[$varname])){
				return $_SESSION[$varname];
			} else{ // end Session is set
				return NULL;
			}
		} // End GET not set
	} // end default type (Get)
else if($type == 'post'){
	if(isset($_POST[$varname])){
			if($_POST[$varname] == $unsetvar){
				unset($_SESSION[$varname]);
				return NULL;
			} else{ // end got unsetvar
				$_SESSION[$varname] = $_POST[$varname];
				return $_POST[$varname];
			}
		} else { // end POST is set
			if(isset($_SESSION[$varname])){
				return $_SESSION[$varname];
			} else{ // end Session is set
				return NULL;
			}
		} // End POST not set
	} // end type post
else{
		if(isset($_REQUEST[$varname])){
			if($_REQUEST[$varname] == $unsetvar){
				unset($_SESSION[$varname]);
				return NULL;
			} else{ // end got unsetvar
				$_SESSION[$varname] = $_REQUEST[$varname];
				return $_REQUEST[$varname];
			}
		} else { // end Request is set
			if(isset($_SESSION[$varname])){
				return $_SESSION[$varname];
			} else{ // end Session is set
				return NULL;
			}
		} // end default type (Request)
	}
}	


// DEPRECIATED - will delete soon
function GetPagePersistent($varname,$type = 'req', $unsetval = '', $page = 0){
	if($page === 0){
		$page = $_SERVER['PHP_SELF'];
	}

	if(isset($_REQUEST[$varname])){
		if($_REQUEST[$varname] == $unsetvar){
			unset($_SESSION[$page]['filters'][$varname]);
			return NULL;
		} else{ // end got unsetvar
			$_SESSION[$page]['filters'][$varname] = $_REQUEST[$varname];
			return $_REQUEST[$varname];
		}
	} else { // end Request is set
		if(isset($_SESSION[$page]['filters'][$varname])){
			return $_SESSION[$page]['filters'][$varname];
		} else{ // end Session is set
			return NULL;
		}
	} // end default type (Request)
}




}



?>